home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / pci_diag_lib.c < prev    next >
C/C++ Source or Header  |  2001-04-11  |  8KB  |  315 lines

  1. ////////////////////////////////////////////////////////////////
  2. // File - PCI_DIAG_LIB.C
  3. //
  4. // Utility functions for printing card information,
  5. // detecting PCI cards, and accessing PCI configuration
  6. // registers.
  7. // 
  8. ////////////////////////////////////////////////////////////////
  9.  
  10. #include "../../include/windrvr.h"
  11. #ifdef _USE_SPECIFIC_KERNEL_DRIVER_
  12.     #undef WD_Open
  13.     #define WD_Open WD_OpenKernelHandle
  14.     #if defined(UNIX)
  15.         #undef WD_FUNCTION
  16.         #define WD_FUNCTION(wFuncNum,h,pParam,dwSize,fWait) \
  17.             ((ULONG) ioctl((int)(h), wFuncNum, pParam))
  18.     #endif
  19. #endif
  20. #include "pci_diag_lib.h"
  21. #include "print_struct.h"
  22. #include <stdio.h>
  23.  
  24. // input of command from user
  25. static char line[256];
  26.  
  27. BOOL PCI_Get_WD_handle(HANDLE *phWD)
  28. {
  29.     WD_VERSION ver;
  30.  
  31.     *phWD = INVALID_HANDLE_VALUE;
  32.     *phWD = WD_Open();
  33.  
  34.     // Check whether handle is valid and version OK
  35.     if (*phWD==INVALID_HANDLE_VALUE) 
  36.     {
  37.         printf("Failed opening " WD_PROD_NAME " device\n");
  38.         return FALSE;
  39.     }
  40.  
  41.     BZERO(ver);
  42.     WD_Version(*phWD,&ver);
  43.     if (ver.dwVer<WD_VER) 
  44.     {
  45.         printf("Incorrect " WD_PROD_NAME " version\n");
  46.         WD_Close (*phWD);
  47.         *phWD = INVALID_HANDLE_VALUE;
  48.         return FALSE;
  49.     }
  50.  
  51.     return TRUE;
  52. }
  53.  
  54. void PCI_Print_card_info(WD_PCI_SLOT pciSlot)
  55. {
  56.     HANDLE hWD;
  57.     WD_PCI_CARD_INFO pciCardInfo;
  58.  
  59.     if (!PCI_Get_WD_handle(&hWD))
  60.         return;
  61.  
  62.     BZERO(pciCardInfo);
  63.     pciCardInfo.pciSlot = pciSlot;
  64.     WD_PciGetCardInfo (hWD, &pciCardInfo);
  65.  
  66.     WD_CARD_print(&pciCardInfo.Card, "   ");
  67.  
  68.     WD_Close (hWD);
  69. }
  70.  
  71. void PCI_Print_all_cards_info() 
  72. {
  73.     HANDLE hWD;
  74.     int i;
  75.     WD_PCI_SCAN_CARDS pciScan;
  76.     WD_PCI_SLOT pciSlot;
  77.     WD_PCI_ID   pciId;
  78.  
  79.     if (!PCI_Get_WD_handle(&hWD)) 
  80.         return;
  81.  
  82.     BZERO(pciScan);
  83.     pciScan.searchId.dwVendorId = 0;
  84.     pciScan.searchId.dwDeviceId = 0;
  85.  
  86.     printf ("Pci bus scan:\n\n");
  87.     WD_PciScanCards (hWD,&pciScan);
  88.  
  89.     for (i=0; i<(int)pciScan.dwCards; i++)
  90.     {
  91.         CHAR tmp[100];
  92.         pciId = pciScan.cardId[i];
  93.         pciSlot = pciScan.cardSlot[i];
  94.         printf("Bus %d Slot %d Function %d, VendorID %04x DeviceID %04x\n",
  95.             pciSlot.dwBus, pciSlot.dwSlot, pciSlot.dwFunction, pciId.dwVendorId,
  96.             pciId.dwDeviceId);
  97.  
  98.         PCI_Print_card_info(pciSlot);
  99.         printf("Press Enter to continue to next slot\n");
  100.         fgets(tmp, sizeof(tmp), stdin);
  101.     }
  102.     WD_Close (hWD);
  103. }
  104.  
  105. DWORD PCI_ReadBytes(HANDLE hWD, WD_PCI_SLOT pciSlot, DWORD dwOffset, 
  106.     DWORD dwBytes)
  107. {
  108.     WD_PCI_CONFIG_DUMP pciCnf;
  109.     DWORD dwVal = 0;
  110.     
  111.     BZERO(pciCnf);
  112.     pciCnf.pciSlot = pciSlot;
  113.     pciCnf.pBuffer = &dwVal;
  114. #if defined(_BIGENDIAN)
  115.     pciCnf.pBuffer = (char *) pciCnf.pBuffer + sizeof(DWORD)-dwBytes;
  116. #endif
  117.     pciCnf.dwOffset = dwOffset;
  118.     pciCnf.dwBytes = dwBytes;
  119.     pciCnf.fIsRead = TRUE;
  120.     WD_PciConfigDump(hWD,&pciCnf);
  121.     return dwVal;
  122. }
  123.  
  124. void PCI_WriteBytes(HANDLE hWD, WD_PCI_SLOT pciSlot, DWORD dwOffset, 
  125.     DWORD dwBytes, DWORD dwData)
  126. {
  127.     WD_PCI_CONFIG_DUMP pciCnf;
  128.     
  129.     BZERO(pciCnf);
  130.     pciCnf.pciSlot = pciSlot;
  131.     pciCnf.pBuffer = &dwData;
  132. #if defined(_BIGENDIAN)
  133.     pciCnf.pBuffer = (char *) pciCnf.pBuffer + sizeof(DWORD)-dwBytes;
  134. #endif
  135.     pciCnf.dwOffset = dwOffset;
  136.     pciCnf.dwBytes = dwBytes;
  137.     pciCnf.fIsRead = FALSE;
  138.     WD_PciConfigDump(hWD,&pciCnf);
  139. }
  140.  
  141. void PCI_EditConfigReg(WD_PCI_SLOT pciSlot)
  142. {
  143.     HANDLE hWD;
  144.     struct 
  145.     {
  146.         CHAR *name;
  147.         DWORD dwOffset;
  148.         DWORD dwBytes;
  149.         DWORD dwVal;
  150.     } fields[30] = {
  151.         { "VID", 0x0, 2 },
  152.         { "DID", 0x2, 2 },
  153.         { "CMD", 0x4, 2 },
  154.         { "STS", 0x6, 2 },
  155.         { "RID", 0x8, 1 },
  156.         { "CLCD", 0x9, 3 },
  157.         { "CALN", 0xc, 1 },
  158.         { "LAT", 0xd, 1 },
  159.         { "HDR", 0xe, 1 },
  160.         { "BIST", 0xf, 1 },
  161.         { "BADDR0", 0x10, 4 },
  162.         { "BADDR1", 0x14, 4 },
  163.         { "BADDR2", 0x18, 4 },
  164.         { "BADDR3", 0x1c, 4 },
  165.         { "BADDR4", 0x20, 4 },
  166.         { "BADDR5", 0x24, 4 },
  167.         { "EXROM", 0x30, 4 },
  168.         { "INTLN", 0x3c, 1 },
  169.         { "INTPIN", 0x3d, 1 },
  170.         { "MINGNT", 0x3e, 1 },
  171.         { "MAXLAT", 0x3f, 1 },
  172.         { NULL, 0, 0 },
  173.         { NULL, 0, 0 }
  174.     };
  175.  
  176.     int cmd;
  177.     int i;
  178.  
  179.     if (!PCI_Get_WD_handle (&hWD)) 
  180.         return;
  181.  
  182.     do
  183.     {
  184.         int row;
  185.         int col;
  186.  
  187.         printf ("\n");
  188.         printf ("Edit PCI configuration registers\n");
  189.         printf ("--------------------------------\n");
  190.         for (row = 0; row<=10; row++)
  191.         {
  192.             for (col = 0; col <=1; col++)
  193.             {
  194.                 if (col==0) i = row;
  195.                 else i = row + 10;
  196.  
  197.                 if (row==10 && col==0)
  198.                 {
  199.                     printf("%26s","");
  200.                 }
  201.                 else
  202.                 {
  203.                     DWORD dwVal;
  204.                     dwVal = PCI_ReadBytes(hWD, pciSlot, fields[i].dwOffset, 
  205.                         fields[i].dwBytes);
  206.                     fields[i].dwVal = dwVal;
  207.                     printf ("%2d. %6s : %0*x %*s    ",i+1, fields[i].name, 
  208.                         fields[i].dwBytes*2, fields[i].dwVal, 
  209.                         8-fields[i].dwBytes*2, "");
  210.                 }
  211.                 if (col==1) printf ("\n");
  212.             }
  213.         }
  214.  
  215.         printf ("99. Back to main menu\n");
  216.         printf ("Choose register to read from / write to, or 99 to exit: ");
  217.         cmd = 0;
  218.         fgets(line, sizeof(line), stdin);
  219.         sscanf(line, "%d",&cmd);
  220.         if (cmd>=1 && cmd <=21)
  221.         {
  222.             i = cmd-1;
  223.             printf ("Enter value to write to %s register (or 'X' to cancel): ",
  224.                 fields[i].name);
  225.             fgets(line, sizeof(line), stdin);
  226.             if (toupper (line[0])!='X')
  227.             {
  228.                 DWORD dwVal;
  229.                 dwVal = 0;
  230.                 sscanf(line,"%x",&dwVal);
  231.                 if ((dwVal>0xff && fields[i].dwBytes==1)|| 
  232.                     (dwVal>0xffff && fields[i].dwBytes==2)|| 
  233.                     (dwVal>0xffffff && fields[i].dwBytes==3))
  234.                 {
  235.                     printf ("Error: value to big for register\n");
  236.                 }
  237.                 else
  238.                 {
  239.                     PCI_WriteBytes(hWD, pciSlot, fields[i].dwOffset, 
  240.                         fields[i].dwBytes, dwVal);
  241.                 }
  242.             }
  243.         }
  244.     } while (cmd!=99);
  245.  
  246.     WD_Close (hWD);
  247. }
  248.  
  249. BOOL PCI_ChooseCard(WD_PCI_SLOT *ppciSlot)
  250. {
  251.     BOOL fHasCard;
  252.     WD_PCI_SCAN_CARDS pciScan;
  253.     DWORD dwVendorID, dwDeviceID;
  254.     HANDLE hWD;
  255.     DWORD i;
  256.  
  257.     if (!PCI_Get_WD_handle (&hWD)) 
  258.         return FALSE;
  259.  
  260.     fHasCard = FALSE;
  261.  
  262.     for (;!fHasCard;) 
  263.     {
  264.         dwVendorID = 0;
  265.         printf ("Enter VendorID: ");
  266.         fgets(line, sizeof(line), stdin);
  267.         sscanf (line, "%x",&dwVendorID);
  268.         if (dwVendorID==0) 
  269.             break;
  270.  
  271.         printf ("Enter DeviceID: ");
  272.         fgets(line, sizeof(line), stdin);
  273.         sscanf (line, "%x",&dwDeviceID);
  274.  
  275.         BZERO(pciScan);
  276.         pciScan.searchId.dwVendorId = dwVendorID;
  277.         pciScan.searchId.dwDeviceId = dwDeviceID;
  278.         WD_PciScanCards (hWD, &pciScan);
  279.         if (pciScan.dwCards==0) // Find at least one card
  280.         {
  281.             printf("Could not find PCI card\n");
  282.         }
  283.         else if (pciScan.dwCards==1)
  284.         {
  285.             *ppciSlot = pciScan.cardSlot[0];
  286.             fHasCard = TRUE;
  287.         }
  288.         else
  289.         {
  290.             printf("Found %d matching PCI cards\n", pciScan.dwCards);
  291.             printf("Select card (1-%d): ", pciScan.dwCards);
  292.             i = 0;
  293.             fgets(line, sizeof(line), stdin);
  294.             sscanf (line, "%d",&i);
  295.             if (i>=1 && i <=pciScan.dwCards)
  296.             {
  297.                 *ppciSlot = pciScan.cardSlot[i-1];
  298.                 fHasCard = TRUE;
  299.             }
  300.             else printf ("Choice out of range\n");
  301.         }
  302.         if (!fHasCard)
  303.         {
  304.             printf ("Do you want to try a different VendorID/DeviceID? ");
  305.             fgets(line, sizeof(line), stdin);
  306.             if (toupper(line[0])!='Y')
  307.                 break;
  308.         }
  309.     }
  310.  
  311.     WD_Close (hWD);
  312.  
  313.     return fHasCard;
  314. }
  315.